<?php
    
    $login = 'gta1807838';
    $pass = '7x62zUN7kT';
    $domain = 's18.hgs.su';
    
    $conn = ftp_connect($domain);
    if (ftp_login($conn, $login, $pass)) {
        $remote_file = 'scriptfiles/chat.txt';
        $handle = fopen('temp_msg', 'w');
        
        if (!in_array($remote_file, (array)ftp_nlist($conn, dirname($remote_file))))
            exit();
        
        if (ftp_fget($conn, $handle, $remote_file, FTP_ASCII, 0)) { // (ftp_nb_get)
            fclose($handle);
            
            $data = array();
            $file = fopen('temp_msg', 'r');
            while (!feof($file)) {
                $mytext = fgets($file, 999);
                $items = explode('|', $mytext);
                $color = $items[0];
                $nick = $items[1];
                $message = iconv('utf-8', 'CP1252', $items[2]);
                $message = iconv('CP1251', 'utf-8', $message);
                
                $data[] = array(
                        "result" => 1,
                        "hash" => md5($color + $nick + $message + time()),
                        "color" => $color,
                        "nick" => $nick,
                        "message" => $message
                          );
            }
            fclose($file);
            ftp_delete($conn, $remote_file);
            
            echo json_encode($data[0]);
            exit();
        }
    else {
        $err = array(
                   "result" => 0
            );
        echo json_encode($err);
    }
        
    }